import {nanoid} from "nanoid";

export default {
    namespaced:true,
    state:{students:[{name:'张三',id:'001'}]},
    actions:{
        addWang(context, data) {
            console.log('调用dispatch,进入actions');
            if (data.indexOf('王') === 0) {
                context.commit('ADDSTU', data);
            }else{
                alert('不姓王,添加失败')
            }
        }
    },
    mutations:{
        ADDSTU(state, data) {
            console.log('调用commit,进入mutations');
            state.students.push({name:data,id:nanoid()});
        }
    },
    getters:{
        countWang(state){
            return state.students.filter((stu)=> stu.name.indexOf('王') === 0).length
        }
    }
}